#!/bin/bash
# init script for the openQRM iscsi-storage-plugin
#
# openQRM Enterprise developed by openQRM Enterprise GmbH.
#
# All source code and content (c) Copyright 2014, openQRM Enterprise GmbH unless specifically noted otherwise.
#
# This source code is released under the GNU General Public License version 2, unless otherwise agreed with openQRM Enterprise GmbH.
# The latest version of this license can be found here: src/doc/LICENSE.txt
#
# By using this software, you acknowledge having read this license and agree to be bound thereby.
#
#           http://openqrm-enterprise.com
#
# Copyright 2014, openQRM Enterprise GmbH <info@openqrm-enterprise.com>
#

OPENQRM_SERVER_BASE_DIR=$(pushd $(dirname $0)/../../../../.. > /dev/null; echo $PWD; popd > /dev/null)
. $OPENQRM_SERVER_BASE_DIR/openqrm/etc/openqrm-server.conf
. $OPENQRM_SERVER_BASE_DIR/openqrm/include/openqrm-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/include/openqrm-server-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/iscsi-storage/include/openqrm-plugin-iscsi-storage-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/iscsi-storage/etc/openqrm-plugin-iscsi-storage.conf



function openqrm_plugin_iscsi_storage_start() {
	openqrm_plugin_iscsi_storage_stop 1>/dev/null 2>&1
	echo "Starting the openQRM iscsi-storage-plugin"
	touch $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/iscsi-storage/.running
	if [ "$ISCSI_STORAGE_AUTO_START" == "yes" ]; then
		if [ -x "/etc/init.d/iscsi-target" ]; then
			if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
				invoke-rc.d iscsi-target start
			else
				/etc/init.d/iscsi-target start
			fi
		elif [ -x /etc/init.d/iscsitarget ]; then
			if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
				invoke-rc.d iscsitarget start
			else
				/etc/init.d/iscsitarget start
			fi
		fi
	fi
}


function openqrm_plugin_iscsi_storage_stop() {
	echo "Stopping the openQRM iscsi-storage-plugin"
	rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/iscsi-storage/.running
	if [ "$ISCSI_STORAGE_AUTO_START" == "yes" ]; then
		if [ -x "/etc/init.d/iscsi-target" ]; then
			if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
				invoke-rc.d iscsi-target stop
			else
				/etc/init.d/iscsi-target stop
			fi
		elif [ -x /etc/init.d/iscsitarget ]; then
			if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
				invoke-rc.d iscsitarget stop
			else
				/etc/init.d/iscsitarget stop
			fi
		fi
	fi
}

function openqrm_plugin_iscsi_storage_uninstall() {
	local USERNAME=$1
	local PASSWORD=$2
	if [ "$USERNAME" == "" ] && [ "$PASSWORD" == "" ]; then
		echo -n "iscsi-storage: Please give a username to uninstall the plugin : "
		read USERNAME
		echo
		echo -n "iscsi-storage: Please give the password for user $USERNAME : "
		read PASSWORD
		echo
	fi
	echo "Uninstalling the openQRM iscsi-storage-plugin"
	openqrm_plugin_iscsi_storage_stop
	openqrm_server_remove_deployment_type $USERNAME $PASSWORD iscsi-deployment
	rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/iscsi-storage
	rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/root-mount.iscsi-deployment
	rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/image.iscsi-deployment.php
	rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/boot-service-iscsi-storage.tgz

	# remove only if lvm-storage is not enabled
	if ! ls $OPENQRM_SERVER_BASE_DIR/openqrm/web/base/plugins | grep lvm-storage 1>/dev/null 2>&1; then
		if ! ls $OPENQRM_SERVER_BASE_DIR/openqrm/web/base/plugins | grep netapp-storage 1>/dev/null 2>&1; then
			rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/iscsid
			rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/iscsiadm
		fi
	fi
}

function openqrm_plugin_iscsi_storage_init() {
	local USERNAME=$1
	local PASSWORD=$2
	if [ "$USERNAME" == "" ] && [ "$PASSWORD" == "" ]; then
		echo -n "iscsi-storage: Please give a username to init the plugin : "
		read USERNAME
		echo
		echo -n "iscsi-storage: Please give the password for user $USERNAME : "
		read PASSWORD
		echo
	fi
	echo "Initializing the openQRM iscsi-storage-plugin"
	openqrm_server_add_deployment_type $USERNAME $PASSWORD iscsi-deployment iscsi-deployment "Iscsi-root deployment" iscsi-storage "Iscsi Enterprise Target Storage"
	# linking the web dir
	ln -sf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/iscsi-storage/web $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/iscsi-storage
	# linking the boot-service
	ln -sf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/iscsi-storage/web/boot-service-iscsi-storage.tgz $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/boot-service-iscsi-storage.tgz
	# linking the root mount hook
	chmod +x $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/iscsi-storage/web/root-mount.iscsi-deployment
	ln -sf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/iscsi-storage/web/root-mount.iscsi-deployment $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/root-mount.iscsi-deployment
	# linking the default image-parameters
	ln -sf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/iscsi-storage/web/image.iscsi-deployment.php $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/image.iscsi-deployment.php

	# copying iscsid to boot-service dir
	if [ ! -x $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/iscsid ]; then
		cp -f `which iscsid` $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/
	fi
	# copying iscsiadm to boot-service dir
	if [ ! -x $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/iscsiadm ]; then
		cp -f `which iscsiadm` $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/
	fi
	# create storage dir for stat files
	mkdir -p $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/iscsi-storage/web/storage
	chmod 777 $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/iscsi-storage/web/storage
	echo "Order deny,allow" > $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/iscsi-storage/web/storage/.htaccess
	echo "Deny from all" >> $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/iscsi-storage/web/storage/.htaccess

}

USER=$2
PASS=$3

case "$1" in
	start)
		openqrm_plugin_iscsi_storage_start
		;;
	stop)
		openqrm_plugin_iscsi_storage_stop
		;;
	restart)
		openqrm_plugin_iscsi_storage_stop
		sleep 1
		openqrm_plugin_iscsi_storage_start
		;;
	init)
		openqrm_plugin_iscsi_storage_init $USER $PASS
		;;
	uninstall)
		openqrm_plugin_iscsi_storage_uninstall $USER $PASS
		;;
	*)
		echo "Usage: $0 {start|stop|restart|init|uninstall}"
		exit 1

esac
exit $?




